#include <mysql/mysql.h>
#include <iostream>
#include <string>

using namespace std;

const char* host = "localhost";
const char* user = "shr";
const char* passwd = "Monster.SHI.413.";
const char* db = "S_C_SC";

class SQL
{
public:
  SQL()
  {
    mysql = mysql_init(&mysql_conn); 
    if(mysql == nullptr) { cout << "mysql init err" << endl; exit(1); }
    MYSQL* retConn = mysql_real_connect(mysql, host, user, passwd, db, 0, NULL, 0);
    if(retConn == nullptr) { cout << "connect err" << endl; exit(1); }
    else { cout << "connect complete" << endl; }
  }

  void ShowTables()
  {

  }

 // remove
 // void SelectAndShowTable()
 // {
 //   const char* query1 = "SELECT * FROM Student";
 //   if(mysql_query(mysql, query1) != 0) { cout << "执行失败" << endl; mysql_close(mysql); exit(1); }
 //   MYSQL_RES* res = mysql_store_result(mysql);
 //   if(res == nullptr) { cout << "result failed" << endl; mysql_close(mysql); exit(1); }
 //   int numRow = mysql_num_rows(res);
 //   int field = mysql_field_count(mysql);
 //   printf("共找到 %d 条结果:\n", numRow);
 //   MYSQL_FIELD* sqlField = mysql_fetch_field(res);
 //   for(int i = 0; i < field; ++i) { printf("%-16s", sqlField[i].name); }
 //   cout << endl;
 //   for(int i = 0; i < numRow; ++i)
 //   {
 //     MYSQL_ROW row = mysql_fetch_row(res);
 //     for(int j = 0; j < field; ++j) { printf("%-15s\t", row[j]); }
 //     cout << endl;
 //   }
 //   mysql_free_result(res);
 // }

  void DescTable()
  {

  }

  void AddOnce()
  {
    
  }

  ~SQL()
  {
    mysql_close(mysql);
    cout << "mysql closed" << endl;
  }

private:
  MYSQL mysql_conn;
  MYSQL* mysql;
};

int main()
{
  SQL sql;
  //sql.SelectAndShowTable();
  //sql.DescTable();
  return 0;
}
